package com.site.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.site.domain.*;
import com.site.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import com.site.util.model.AjaxModel;

/**
 * Purpose: 首页
 * @author: liangjun
 * @since: JDK 1.6
 * @date: 2015年9月25日
 */
@Controller
@RequestMapping("/index/")
public class IndexController {
	private static Logger logger = LoggerFactory.getLogger(IndexController.class);

	@Autowired
	private BannerInfoService bannerInfoService;
	@Autowired
	private CoreBusinessService coreBusinessService;
	@Autowired
	private TypicalCasesService typicalCasesService;
	@Autowired
	private RecruitInfoService recruitInfoService;
	@Autowired
	private InfoDetailsService infoDetailsService;

	private List<BannerInfo> bannerInfoList = new ArrayList();
	private List<CoreBusiness> coreBusinessList = new ArrayList();
	private List<TypicalCases> typicalCasesList = new ArrayList();
	private List<RecruitInfo> recruitInfoList = new ArrayList();
	private List<InfoDetails> infoDetailsList = new ArrayList();
	
	/**
	 * Purpose: 初始加载
	 * @author: liangjun
	 * @return: void
	 */
	public void init(){
		try {
			this.bannerInfoList = this.bannerInfoService.findBannerInfo();
			this.coreBusinessList = this.coreBusinessService.findCoreBusiness();
			this.typicalCasesList = this.typicalCasesService.findTypicalCases();
			this.recruitInfoList = this.recruitInfoService.findRecruitInfo();
			this.infoDetailsList = this.infoDetailsService.findInfoDetails();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("初始化加载异常："+e.getMessage()+"");
		}
	}
	
	/**
	 * Purpose: 首页
	 * @author: liangjun
	 * @param request
	 * @param response
	 * @return: ModelAndView
	 */
	@RequestMapping(value="index",method=RequestMethod.GET)
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("bannerInfoList", this.bannerInfoList);
		mav.addObject("coreBusinessList", this.coreBusinessList);
		mav.addObject("typicalCasesList", this.typicalCasesList);
		mav.addObject("recruitInfoList", this.recruitInfoList);
		mav.addObject("infoDetailsList", this.infoDetailsList);
		return mav;
	}
	
	/**
	 * Purpose: 经典案例
	 * @author: liangjun
	 * @param request
	 * @param response
	 * @return: ModelAndView
	 */
	@RequestMapping(value="typical",method=RequestMethod.GET)
	public ModelAndView typical(HttpServletRequest request,HttpServletResponse response) {
		return index(request, response);
	}
	
	/**
	 * Purpose: 加入我们（招聘）
	 * @author: liangjun
	 * @param request
	 * @param response
	 * @return: ModelAndView
	 */
	@RequestMapping(value="recruit",method=RequestMethod.GET)
	public ModelAndView recruit(HttpServletRequest request,HttpServletResponse response) {
		return index(request, response);
	}
	
	/**
	 * Purpose: 关于我们
	 * @author: liangjun
	 * @param request
	 * @param response
	 * @return: ModelAndView
	 */
	@RequestMapping(value="abouts",method=RequestMethod.GET)
	public ModelAndView abouts(HttpServletRequest request,HttpServletResponse response) {
		return index(request, response);
	}
	
	/**
	 * Purpose: 核心业务
	 * @author: liangjun
	 * @param request
	 * @param response
	 * @return: ModelAndView
	 */
	@RequestMapping(value="business",method=RequestMethod.GET)
	public ModelAndView business(HttpServletRequest request,HttpServletResponse response) {
		return index(request, response);
	}
	
	/**
	 * Purpose: 公告信息
	 * @author: liangjun
	 * @param request
	 * @param response
	 * @return: ModelAndView
	 */
	@RequestMapping(value="info",method=RequestMethod.GET)
	public ModelAndView info(HttpServletRequest request,HttpServletResponse response) {
		return index(request, response);
	}
	
	/**
	 * Purpose: 刷新banner缓存
	 * @author: liangjun
	 * @param request
	 * @param response
	 * @return: void
	 */
	@ResponseBody
	@RequestMapping(value="refreshBanner",method=RequestMethod.POST)
	public AjaxModel refreshBanner(HttpServletRequest request,HttpServletResponse response) {
		AjaxModel aml = new AjaxModel();
		int retCode = 0;
		String retMsg = "";
		UserInfo userInfo = null;
		userInfo = (UserInfo) WebUtils.getSessionAttribute(request, "userInfo");
		if(null == userInfo){
			aml.setRetCode(-1);
			aml.setRetMsg("请重新登录");
			return aml;
		}
		try {
			bannerInfoList = bannerInfoService.findBannerInfo();
			retCode = 1;
			retMsg = "刷新成功";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("刷新banner缓存异常："+e.getMessage()+"");
			retMsg = "刷新缓存异常";
		}
		aml.setRetCode(retCode);
		aml.setRetMsg(retMsg);
		return aml;
	}

	@ResponseBody
	@RequestMapping(value = "refreshCore", method = {RequestMethod.POST})
	public AjaxModel refreshCore(HttpServletRequest request, HttpServletResponse response) {
		AjaxModel aml = new AjaxModel();
		int retCode = 0;
		String retMsg = "";
		UserInfo userInfo = null;
		userInfo = (UserInfo)WebUtils.getSessionAttribute(request, "userInfo");
		if (null == userInfo) {
			aml.setRetCode(-1);
			aml.setRetMsg("请重新登录");
			return aml;
		} else {
			try {
				this.coreBusinessList = this.coreBusinessService.findCoreBusiness();
				retCode = 1;
				retMsg = "刷新成功";
			} catch (Exception var8) {
				var8.printStackTrace();
				logger.error("刷新核心业务缓存异常：" + var8.getMessage() + "");
				retMsg = "刷新缓存异常";
			}

			aml.setRetCode(retCode);
			aml.setRetMsg(retMsg);
			return aml;
		}
	}

	/**
	 * Purpose: 刷新经典案例缓存
	 * @author: liangjun
	 * @param request
	 * @param response
	 * @return: void
	 */
	@ResponseBody
	@RequestMapping(value="refreshTypical", method=RequestMethod.POST)
	public AjaxModel refreshTypical(HttpServletRequest request,HttpServletResponse response) {
		AjaxModel aml = new AjaxModel();
		int retCode = 0;
		String retMsg = "";
		UserInfo userInfo = null;
		userInfo = (UserInfo) WebUtils.getSessionAttribute(request, "userInfo");
		if(null == userInfo){
			aml.setRetCode(-1);
			aml.setRetMsg("请重新登录");
			return aml;
		}
		try {
			typicalCasesList = typicalCasesService.findTypicalCases();
			retCode = 1;
			retMsg = "刷新成功";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("刷新经典案例缓存异常："+e.getMessage()+"");
			retMsg = "刷新缓存异常";
		}
		aml.setRetCode(retCode);
		aml.setRetMsg(retMsg);
		return aml;
	}
	
	/**
	 * Purpose: 刷新招聘缓存
	 * @author: liangjun
	 * @param request
	 * @param response
	 * @return: void
	 */
	@ResponseBody
	@RequestMapping(value="refreshRecruit",method=RequestMethod.POST)
	public AjaxModel refreshRecruit(HttpServletRequest request,HttpServletResponse response) {
		AjaxModel aml = new AjaxModel();
		int retCode = 0;
		String retMsg = "";
		UserInfo userInfo = null;
		userInfo = (UserInfo) WebUtils.getSessionAttribute(request, "userInfo");
		if(null == userInfo){
			aml.setRetCode(-1);
			aml.setRetMsg("请重新登录");
			return aml;
		}
		try {
			recruitInfoList = recruitInfoService.findRecruitInfo();
			retCode = 1;
			retMsg = "刷新成功";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("刷新招聘缓存异常："+e.getMessage()+"");
			retCode = 0;
			retMsg = "刷新缓存异常";
		}
		aml.setRetCode(retCode);
		aml.setRetMsg(retMsg);
		return aml;
	}

	/**
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "refreshInfo", method = {RequestMethod.POST})
	public AjaxModel refreshInfo(HttpServletRequest request, HttpServletResponse response) {
		AjaxModel aml = new AjaxModel();
		int retCode = 0;
		String retMsg = "";
		UserInfo userInfo = null;
		userInfo = (UserInfo)WebUtils.getSessionAttribute(request, "userInfo");
		if (null == userInfo) {
			aml.setRetCode(-1);
			aml.setRetMsg("请重新登录");
			return aml;
		} else {
			try {
				this.infoDetailsList = this.infoDetailsService.findInfoDetails();
				retCode = 1;
				retMsg = "刷新成功";
			} catch (Exception var8) {
				var8.printStackTrace();
				logger.error("刷新信息披露缓存异常：" + var8.getMessage() + "");
				retMsg = "刷新缓存异常";
			}

			aml.setRetCode(retCode);
			aml.setRetMsg(retMsg);
			return aml;
		}
	}
	
}
